#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include "QRegExpValidator"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);//打开清空按钮


    ui->lineEdit_iphone->setPlaceholderText("请输入电话");
    ui->lineEdit_iphone->setClearButtonEnabled(true);//打开清空按钮

    //Iputmask太局限
    //ui->lineEdit_iphone->setInputMask("000-0000-0000");//太局限

    //使用正则表达式
    QRegExp regExp("^1\\d{10}$");
    ui->lineEdit_iphone->setValidator(new QRegExpValidator(regExp));


    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setClearButtonEnabled(true);//打开清空按钮
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";

    qDebug()<< ui->lineEdit_name->text() << " "
            << ui->lineEdit_iphone->text() << " "
            << gender << " "
            << ui->lineEdit_password->text() ;
}

void Widget::on_lineEdit_iphone_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_iphone->validator()->validate(content, pos) == QValidator::Acceptable)
    {
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setEnabled(false);
    }
}

//添加显示密码
void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    }
}
